home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 312_01 / cincdep.awk < prev    next >
Text File  |  1990-04-21  |  16KB  |  336 lines

  1. #  HEADER:        ;
  2. #  TITLE:         Makefile dependency generator;
  3. #  DATE:          09/28/1989;
  4. #  VERSION:       2.2;
  5. #  DESCRIPTION:   "An AWK program which finds included files and
  6. #                 builds a dependency list.  This dependency list
  7. #                 is a component of a make-file.  The list consists
  8. #                 of the source file name followed by a colon and
  9. #                 then all of the files which it includes (and also
  10. #                 those which are included by the included files, &c.)
  11. #                 The last item is a compile-command.";
  12. #
  13. #  KEYWORDS:      Makefile, make, include, dependency generator;
  14. #  SYSTEM:        MS-DOS, UNIX;
  15. #  WARNINGS:      "Doesn't handle directory names embedded within
  16. #                 #include <here>.  Doesn't handle preprocessor #if.. blocks
  17. #                 or #include statements which are "commented out."
  18. #                 Only handles a single include-path.  
  19. #                 PolyAwk will generate false matches to lines
  20. #                 beginning with non-ASCII (i.e. 128..256) chars."
  21. #                 Filenames are case sensitive.";
  22. #  FILENAME:      CINCDEP.AWK;
  23. #  SEE-ALSO:      TLR2MAK.AWK, CF2MAK.AWK, AINCDEP.AWK, PINCDEP.AWK;
  24. #  AUTHORS:       James Yehle;
  25. #  COMPILERS:     PolyAWK, Mortice Kern AWK, Rob Duff's PC-AWK 2.0,
  26. #                 In any case, must be 1985 awk (not 1977);
  27. #
  28. # ============================================================================
  29. #
  30. # cincdep.awk         Last modified  Sep 28, 1989  22:23
  31. #
  32. #   Scans a C file for all "#include" dependencies
  33. #   This scanning process involves all nested include files, too.
  34. #
  35. #  Jim Yehle  753 Left Fork Rd. #B/Sugarloaf Star Route/Boulder, CO 80302 9252
  36. # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  37. #
  38. #   Usage is:
  39. #     awk -f [path]cincdep.awk [path]src_file.c objname=[path]filename
  40. #        cc=compile_comd ccf=[p][n][e] [incpath=[pathname]] [>[path]outfile]
  41. #     Pathnames must contain trailing separator, e.g. "c:\inc\"
  42. #     Order of command-line parameters (save for "-f ..." and srcfile.c)
  43. #      is irrelevant, but identifiers must be in lower case.
  44. #     "src_file.c" may be replaced by special "__NOFILE__"
  45. #     cc is the compile command (no spaces allowed)
  46. #     ccf controls which components of the source file's name get
  47. #         included in the compile command line: p = directory path,
  48. #         n = name, e = extension.
  49. #
  50. #   Output is:
  51. #     [path]src_file.obj: [path]src_file.c [path]included_file1 linecont
  52. #                         [path]included_file2 [path]included_file3 ...
  53. #     <\t>comd [path]src_file[extenstion]
  54. #     <\n>
  55. #   For __NOFILE__, output is:
  56. #     [path]src_file.obj:
  57. #     <\n>
  58. #     <\n>
  59. # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  60. #
  61. # Known limitations and anomalies:
  62. # - Assumes that no directory paths are within the "file" or <file> strings
  63. # - Doesn't make any attempt to switch out #if-#endif blocks.
  64. #    I don't see this as a serious problem.  (The solution certainly isn't
  65. #    trivial.)  It will mean only that your make-file may turn out to be a
  66. #    bit more "paranoid," including more dependencies than really exist
  67. #    if an #include directive is #iffed out.  It could be a problem if 
  68. #    nonexistent include-files appear in a make-file's dependency, and your
  69. #    make utility barfs when it doesn't find a file.  
  70. # - The same problem can be created by "commenting out" a line containing
  71. #    an include directive, such as:
  72. #       /*
  73. #       #include <stdio.h>
  74. #       */
  75. # - File names are case- and directory-sensitive.  Huh?  If a file includes
  76. #    both blah.h and Blah.h, they'll both appear in the dependency list.
  77. #    Furthermore, if it includes "splunge.h" and <splunge.h>, and an instance
  78. #    appears in both the default directory and the include-file directory,
  79. #    you'll get two occurrences in the dependency list, one with the 
  80. #    directory pathname tacked on the front and the other without.
  81. # - So far it has been tested with Polytron and MKS awk interpreters, and
  82. #    Turbo C's make utility, all running under the MS-DOS operating system.
  83. # - Your ability to specify complex compilation commands is really limited.
  84. #    There's also no way to set source-file-specific command-line options.
  85. # - I don't know what will happen if your source file doesn't end with .c
  86. #    and your object files don't end with .obj.  Try it!
  87. # - Only one include-path is supported.  For the form #include <file>, cincdep
  88. #    searches the include-path first, then the current directory for the
  89. #    given file.  If your file says #include "file", the order is reversed.
  90. #
  91. # . . . Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . .
  92. #
  93. #        Sep 28  89            -- Released for C User's Group distribution --
  94. #  2.2   Sep 10  89       JRY  Added legal-filename-char regular expression
  95. #  2.1   Aug 17  89       JRY  Added add_fnexist option (as internal directive)
  96. #  2.0   Aug 10  89       JRY  If an included file can't be found, cincdep now
  97. #                               beeps and prints an error to CO, then continues
  98. #                               without scanning the file for nested inclusions.
  99. #                               (It adds it to the dependency list, without a
  100. #                               directory path on the front of the filename.)
  101. #                               Also added an optimization:  if addfile() sees
  102. #                               that a file is already in the list, then it has
  103. #                               already been scanned, so it isn't done again.
  104. #                               Added "verbose" CO output as debug level 1.
  105. #  1.7   Jun 20  89       JRY  Added "ccf=" command-line parameter
  106. #  1.6   Jun 7   1989     JRY  Added include-file pathnames to output,
  107. #                               added objname= & cc= command-line parameters
  108. #  1.5   May 31  1989     JRY  Added primary source file at start of
  109. #                               dependency list
  110. #  1.4  Cinco di Mayo 89  JRY  Cleaned output-line-too-long test
  111. #  1.3   Apr 21  1989     JRY  Added line-too-long continuation on next line,
  112. #                               translation of primary file from .c to .obj.
  113. #  1.2   Feb 22  1989     JRY  Added incpath command-line option
  114. #  1.1   Feb 21  1989     JRY  Re-wrote to handle recursion internally.
  115. #  1.0   Feb 15  1989     JRY  Initial creation & debugging.. algorithm used
  116. #                               recursive invocations of awk via system(awk)
  117. #                               function, but ran out of memory (640k DOS)
  118. #                               after three levels.
  119. # ============================================================================
  120. #
  121.  
  122. BEGIN {
  123.    co = "CON"       # Console-out: MS-DOS "CON", iRMX ":co:", unix "/dev/tty"
  124.    linecont = " \\" # Line-continuation EOL char: snake " /", unix make "\\"
  125.    debug = 0        # 0=off, 1=verbose, 2=main-level debug, 3= adds functions
  126.    use_fnexist = 1  # Add nonexistent header files to dependency list?
  127.    fname_chars = "[^\\\\ :]" # Any single legal char in a file name.
  128.    #  (Must exclude directory path separator character! (DOS \, UNIX /) )
  129.  
  130.    printf( "cincdep.awk  C #include dependency scanner  v 2.2") > co
  131.    objfile = get_cl_param( "objname=", 0)
  132.    if (debug>1) printf( "objfile = '%s'\n", objfile) > co
  133.    cc = get_cl_param( "cc=", 0)
  134.    if (debug>1) printf( "cc = '%s'\n", cc) > co
  135.    ccf = get_cl_param( "ccf=", 0)
  136.    if (debug>1) printf( "ccf = '%s'\n", ccf) > co
  137.    # Omitting "incpath=xx" ok.. assume default directory for #include files
  138.    incpath = get_cl_param( "incpath=", 1)
  139.    if (debug>1) printf( "incpath = '%s'\n", incpath) > co
  140.    if (ARGV[1] == "__NOFILE__") {
  141.       if (debug)                    
  142.          printf( "\n__NOFILE__: Empty dependency list generated.\n") > co
  143.       else
  144.          printf( "  (of __NOFILE__)\n") > co
  145.       exit # Hop to END; srcfile is still an empty string & flist is empty
  146.    }
  147.    # 1st in list of ARGV[]'s is sourc